package algorithm;

import structure.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 完全二叉树的节点个数
 */
public class LeetCode_countNodes_222 {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            count += size;
            for (int i = 0 ; i < size; i++) {
                TreeNode curr = queue.poll();
                if (curr.left != null) {
                    queue.add(curr.left);
                }
                if (curr.right != null) {
                    queue.add(curr.right);
                }
            }
        }
        return count;
    }
}
